package nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ReadAndWriteAudioFile {
	public static void main(String args[]){
		File filein = new File("file/audio-in.mp3");
		File fileout = new File("file/audio-out-nio.mp3");
		try {
			RandomAccessFile fin = new RandomAccessFile(filein, "r");
			FileOutputStream fout = new FileOutputStream(fileout);
			FileChannel inChannel = fin.getChannel();
			FileChannel outChannel = fout.getChannel();
			
			ByteBuffer buf = ByteBuffer.allocate(1024);
			
			int bytesRead = inChannel.read(buf);
			while(bytesRead != -1){
				buf.flip();

				outChannel.write(buf);
				buf.clear();
				bytesRead = inChannel.read(buf);
			}
			fin.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
